<?php
/**
 *快聚fun聚会通知函页面
 */
include 'inc/common.inc.php';
include 'Minvitation.class.php';
include 'authcode.class.php';
include 'MCheckUser.class.php';

class Controller extends BaseController
{
	public $gatherId;
	public $userTel;

	
	/**
	 *检查用户cookie信息
	 *@return boolean
	 */
	public function checkCookie()
	{
		if(isset($_SESSION['session_id']))
		{
			$check = new MCheckUser();
			if($check->checkSessionId())
			{
				return true;
			}else{
				return false;
			}
		}else{
			return true;
		}
	}
	
	/**
	 *移动终端获取基本信息
	 *(待做)
	 */
	 
	/*
	public function mobileTerminal()
	{
		$userTel = $this->get_request("");
		$gatherId = $this->get_request("");
		if($userTel!="" && $gatherId!="")
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	*/
	
	/**
	 *解密gather参数中的值
	 */
	public function getInfor()
	{
		$temp = $this->get_request("gather");
		$authcode = new authcode();
		$decode = $authcode->keycode($temp,"DECODE");
		$tmp = explode("#",$decode);
		$this->gatherId = $tmp[0];
		$this->userTel = $tmp[1];
	}
	
	/**
	 *接收聚会id,初始化聚会信息
	 *@return initializeInform
	 */
	public function informInitialize()
	{
		$this->getInfor();
		$gatherId = $this->gatherId;
		$userTel = $this->userTel;
		$db = new Minvitation();
		$optionalDate = $db->getOptionalDate($gatherId);
		$initializeInform = $db->getInitializeInform($gatherId);
		$lastSelect = $db->getLastSelect($gatherId,$userTel);
		$attendPeople = $db->getPeople($gatherId);
		$initializeInform = array("initializeInform"=>$initializeInform,"attendPeople"=>$attendPeople,"optionalDate"=>$optionalDate,"lastSelect"=>$lastSelect);
		return $initializeInform;
	}
	
	/**
	 *接收用户返回的空闲日期
	 *@return header("Location: launch_party.php");
	 */
	public function receiveDateAction()
	{
		$this->getInfor();
		$db =  new Minvitation();
		if($this->get_cookie("userTel")!=null)
		{
			$userTel = $this->get_cookie("userTel");
		}
		else if($this->userTel!=null)
		{
			$userTel = $this->userTel;
		}
		$gatherId = $this->get_request("gatherId");
		$whether = $this->get_request("whether");
		$freeDate = $this->get_request("freeDate");
		$lastSelect = $this->get_request("lastSelect");
		if($whether=="yes"){
			$freeDateArray = explode(";",$freeDate);
			$lastSelectArray = explode(";",$lastSelect);
			$newDate = array_diff($freeDateArray,$lastSelectArray);
			$delDate = array_diff($lastSelectArray,$freeDateArray);
			$temp = array();
			foreach($newDate as $key=>$value)
			{
				array_push($temp,$value);
			}
			$newDate = $temp;
			$temp = array();
			foreach($delDate as $key=>$value)
			{
				array_push($temp,$value);
			}
			$delDate = $temp;
			$count = count($newDate);
			$db->updateState($gatherId,$userTel);
			for($num=0;$num<$count;$num++)
			{
				$db->insertFreeDate($userTel,$gatherId,$newDate[$num]);
				$db->addAttendNum($gatherId,$newDate[$num]);
			}
			$count = count($delDate);
			for($num=0;$num<$count;$num++)
			{
				$db->deleteFreeDate($userTel,$gatherId,$delDate[$num]);
				$db->delAttendNum($gatherId,$delDate[$num]);
			}
		}else if($whether=="no"){
			$lastSelectArray = explode(";",$lastSelect);
			$db->updateNoGo($gatherId,$userTel,$lastSelectArray);
		}
		
		//header("Location: launch_party.php");
	}
	
}

$c = new Controller();
$c->run();
// $temp = new authcode();
// $str = "85#15214352781";
// echo $str = $temp->keycode($str,'ENCODE');
// echo "<br>";
// echo $str = $temp->keycode($str,'DECODE');
// echo "<br>";
if($c->checkCookie())
{
	$informInitialize = $c->informInitialize();
	$gatherId = $c->gatherId;
	$name = $informInitialize['initializeInform']["name"];
	$theme = $informInitialize['initializeInform']["theme"];
	$attendPeople = $informInitialize['attendPeople'];
	$startTime = $informInitialize['initializeInform']["startTime"];
	$startTime = substr($startTime,0,5);
	$rangeDate = $informInitialize['initializeInform']["FirstDate"]."-".$informInitialize['initializeInform']["LastDate"];
	$verify = $c->verify;
	$nowTime = date("Y-m-d");
	$nowTime = strtotime($nowTime);
	if(isset($informInitialize['optionalDate']))
	{
		$optionalDate = "";
		foreach($informInitialize['optionalDate'] as $key => $value)
		{
			$optionalDate .= (strtotime($value['holdDate']) - $nowTime)/3600/24 .";";			
		}
	}
	if(isset($informInitialize['lastSelect']))
	{
		$lastSelect = "";
		foreach($informInitialize['lastSelect'] as $key => $value)
		{
			$lastSelect .= $informInitialize['lastSelect'][$key]['freeDate'] .";";
		}
	}
	include template('invitation.html');
}
else
{
	header("Location: index.php");
}
?>
